﻿//function generateViewModel(viewModelJs) {

//    for (var i in viewModelJs.Events) {
//        viewModelJs.Events[i].Participants = participants[viewModelJs.Events[i].EventId];
//    };

//    viewModel = ko.mapping.fromJS(viewModelJs);
//    applyGlobalBindings(viewModel);

//    ko.applyBindings(viewModel);
    
//    return viewModel;
//}

function generateViewModel(viewModelJs) {

    viewModel = ko.mapping.fromJS(viewModelJs);
    applyGlobalBindings(viewModel);

    ko.applyBindings(viewModel);

    return viewModel;
}

function applyGlobalBindings(viewModel) {

    //for (var j in viewModel.Participants()) {

    //    var participant = viewModel.Participants()[j];
    //    participant.FullName = ko.computed({
    //        read: function () {
    //            return this.Name() + ' ' + this.Surname();
    //        },
    //        owner: participant
    //    });
    //}

    //viewModel.putParticipant = function (eventId) {
    //    $.ajax({
    //        type: "POST",
    //        url: "/api/Participants?user=moretto.carlo@outlook.com" + "&eventId=" + eventId,
    //        success: function (data) {
    //            toastr.success(' data saved.');
    //            updateJsSingleMonth(index, data);
    //        },
    //        error: function () {
    //            toastr.error(' not saved.');
    //        }
    //    });

    //}

    for (var j in viewModel.Events()) {

        var event = viewModel.Events()[j];
        var d = new Date(event.DateTo());
        event.DateTo(d.toUTCString());
    }

    for (var j in viewModel.Events()) {
        var event = viewModel.Events()[j];
        event.DateFromComputed = ko.computed({
            read: function () {
                var d = new Date(this.DateFrom());
                return d.toUTCString();
            },
            owner: event
        });
    }
    
}



function pause(milliseconds) {
    var dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}